package main

import (
	"encoding/hex"
	"fmt"
	"math/big"

	"gitee.com/gogm/gmsm/sm2"
)

type Sm2Signature struct {
	R, S *big.Int
}

func ServerKeyExchange() {
	client_random := "2a59bd8c4a18fddff05d1b7a122ec417ee11b468c32cb52ea639a83320002c22"
	server_random := "b76a72d7c7980509c294902d69ccc5dbd85f755704788e6ad03247da4bdab3c4"
	se_len := "00021f"
	se_cert_hex := "3082021b308201c0a003020102020900d21766be4a85d0a5300a06082a811ccf55018375308182310b300906035504061302434e310b300906035504080c02424a3110300e06035504070c074861694469616e31253023060355040a0c1c4265696a696e67204a4e544120546563686e6f6c6f6779204c54442e31153013060355040b0c0c534f5242206f6620544153533116301406035504030c0d546573742043412028534d3229301e170d3230313031393033353432395a170d3234313132373033353432395a308185310b300906035504061302434e310b300906035504080c02424a3110300e06035504070c074861694469616e31253023060355040a0c1c4265696a696e67204a4e544120546563686e6f6c6f6779204c54442e31153013060355040b0c0c42535243206f6620544153533119301706035504030c1073657276657220656e632028534d32293059301306072a8648ce3d020106082a811ccf5501822d0342000462d5174ec18e73eb21e30a216f78b3838354b072eb109ec2013b1ee84243c7b9ec859d92dddff40025955161142509f2516378455ff0614ebbf33259456e8014a31a301830090603551d1304023000300b0603551d0f040403020338300a06082a811ccf55018375034900304602210093be57180f18847a57c7f96ac97c3349a985f1d56b5bbf22598c57f7586f1925022100e696f53e1a7c2a89a1b16a7423949068e52bf3d503c556acc3fdc850c88b9f0e"

	//ss服务端证书
	ss_cert_hex := "3082021c308201c1a003020102020900d21766be4a85d0a4300a06082a811ccf55018375308182310b300906035504061302434e310b300906035504080c02424a3110300e06035504070c074861694469616e31253023060355040a0c1c4265696a696e67204a4e544120546563686e6f6c6f6779204c54442e31153013060355040b0c0c534f5242206f6620544153533116301406035504030c0d546573742043412028534d3229301e170d3230313031393033353432395a170d3234313132373033353432395a308186310b300906035504061302434e310b300906035504080c02424a3110300e06035504070c074861694469616e31253023060355040a0c1c4265696a696e67204a4e544120546563686e6f6c6f6779204c54442e31153013060355040b0c0c42535243206f662054415353311a301806035504030c11736572766572207369676e2028534d32293059301306072a8648ce3d020106082a811ccf5501822d03420004e28cbdcfc800c2c9f9b7dc6aa9f22b60f3a8d4641c9bb35d3ee4ad16173c07bce3c1d1cfa70d99f4993429a2f91f737b39f0be24dfa295fc7c73f8f5bf301cfda31a301830090603551d1304023000300b0603551d0f0404030206c0300a06082a811ccf550183750349003046022100f80f203154fe747dff0c5cf5a7cb0fcc8513f1398253a40cc9c52ee99005a907022100e0e729411d04a008584e8ae47109145dd7f14efc2e1822c9aef0d833905ce06b"
	//签名的结果数据
	verify_out_hex := "3045022100abc180fb924268ece1c501a958f39d444190a4ad67da1a791c330907acc5aab7022077ae21f3a968999917136abbca38354fc3d9d4b030e14f425253575e71be18cd"

	signture_data_hex := client_random + server_random + se_len + se_cert_hex
	signture_data, _ := hex.DecodeString(signture_data_hex)
	verify_out, _ := hex.DecodeString(verify_out_hex)
	ss_cert_data, _ := hex.DecodeString(ss_cert_hex)

	ss_cert, err := sm2.ParseCertificate(ss_cert_data)
	if err != nil {
		fmt.Println("sm2 ParseCertificate err->", err)
		return
	}

	//解析出公钥
	pubk, ok := ss_cert.PublicKey.(*sm2.PublicKey)
	fmt.Println("ok->", ok)

	if !ok {
		fmt.Println("ok pubk->", pubk)
		//		fmt.Println("ok pub X->", pubk.X.Text(16))
		//		fmt.Println("ok pub Y->", pubk.Y.Text(16))
		return
	}
	fmt.Println("Verify out->", pubk.Verify(signture_data, verify_out))
}

func main() {
	ServerKeyExchange()

}
